<% layout('/layouts/default.html', {title: '应收单管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(tar.isNewRecord ? '新增应收单' : '编辑应收单')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${tar}" action="${ctx}/ar/tar/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="arId"/>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="arCode" maxlength="100" class="form-control" readOnly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('客户')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="customerId" items="${listCu}"
								itemLabel="customerName" itemValue="customerId" blankOption="true" class="form-control" onchange="getByCuId()"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="date" readonly="true" maxlength="20" class="form-control laydate required"
									dataFormat="date" data-type="date" data-format="yyyy-MM-dd" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('状态')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="status1" dictType="receivable_bill_type" blankOption="true" class="form-control" readonly="true"/>
								<input type="hidden" name="status1" value="1">
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('服务订单编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="serviceOrderId" items="${serviceOrderList}"
								itemLabel="serviceOrderCode" itemValue="serviceOrderId" dictType="" blankOption="true" class="form-control" onchange="getCurrencyOrderId()" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('收款名字')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="paymentAccount" maxlength="100" class="form-control" />
								<input type="hidden" name="paymentAccount" value="">
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('金额')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="amount" class="form-control number" readOnly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('币种')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="currency" dictType="sys_bizhongy" class="form-control"  blankOption="false"/>
								<input type="hidden" name="currency" value="">
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('前置单据类型')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="parentBillType" dictType="" blankOption="true" class="form-control" disabled="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('前置单据编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="parentBillCode" dictType="" blankOption="true" class="form-control" disabled="true"/>
							</div>
						</div>
					</div>

				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注信息')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-unit">${text('费用明细')}</h4>
				<div class="ml10 mr10">
					<table id="tarDetailDataGrid"></table>
					<% if (hasPermi('ar:tar:edit')){ %>
						<a href="#" id="tarDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
					<h4 class="form-unit">${text('系统信息')}</h4>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('创建者')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="createBy" maxlength="64" class="form-control" disabled="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('创建时间')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="createDate" maxlength="64" class="form-control"
								dataFormat="date" data-type="date" data-format="yyyy-MM-dd"
								readOnly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('更新者')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="updateBy" maxlength="64" class="form-control" disabled="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('更新时间')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="createDate" maxlength="64" class="form-control"
								dataFormat="date" data-type="date" data-format="yyyy-MM-dd"
								disabled="true"/>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('ar:tar:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>

		</#form:form>
	</div>
</div>
<% } %>
<script>

	function getCurrencyOrderId() {
		var str=":;";
		if ($("#serviceOrderId").val() != "") {
			$.ajax({
				type: "POST",
				async : false,
				url: "${ctx}/ar/tar/getCurrencyOrderId",
				data: {"serviceOrderId": $("#serviceOrderId").val()},
				dataType : "json",
				success: function (data) {
					if (data!= "") {
						$('#currency').select2('val', data.currency);
						//根据订单编号赋值车架号
						if (data.length > 0) {

							for (var i = 0; i < data.length; i++) {
								var frameNumber1 = data[i].rowNumber;
								var frameNumberId = data[i].serviceOrderDetailId;
								if(i!=data.length-1){
									str+=frameNumberId+":"+frameNumber1+";";
								}else {
									str+=frameNumberId+":"+frameNumber1;// 这里是option里面的 value:label
								}
							}
						} else{
							js.showErrorMessage("该订单没有行号信息！");
						}
					} else{
						js.showErrorMessage("未设置币种！");
					}
					// alert(str);
					$('#tarDetailDataGrid').setColProp('rowNumber1', {editoptions: {value:str}});
					$('#tarDetailDataGrid').dataGrid('reloadGrid');

				}
			});
		}
	}


//初始化应收单明细DataGrid对象
$("#tarDetailDataGrid").dataGrid({

	data: ${toJson(tar.tarDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'arDetailId', editable:true, hidden:true},
		{header:'上传的行号', name:'rowNumber', width:150,hidden:true, editable:true, edittype:'text', editoptions:{'maxlength':'2', 'class':'form-control'}},
		{header:'行号', name:'rowNumber1',width:150, editable:true,edittype:'select',editoptions:{'class':'form-control',
				items: ${toJson(rowNumberList)},
				itemLabel: 'rowNumber', itemValue: 'rowNumber',
				dataEvents: [
					{type: 'change',
						fn: function(e) {
							var that=$(this);
							$.ajax({
								type: "POST",
								async : false,
								url: "${ctx}/ar/tar/getTarDetailByDetailId",
								data: {"tarDeatilId":$(this).val()},
								dataType : "json",
								success: function (data) {
									if (data != null) {
										var rowid = $("#tarDetailDataGrid").jqGrid("getGridParam", "selrow");//获取行号
										that.closest('tr').find("input[name=\"frameNumber\"]").val(data.frameNumber);
										that.closest('tr').find("input[name=\"rowNumber\"]").val(data.rowNumber);
										that.closest('tr').find("select[name=\"brand\"]").val(data.brand);
										that.closest('tr').find("select[name=\"area\"]").val(data.area);
										that.closest('tr').find("input[name=\"model\"]").val(data.model);
										that.closest('tr').find("input[name=\"carId\"]").val(data.carId);
										that.closest('tr').find("input").attr("readonly","readonly");
										that.closest('tr').find("input[name=\"apAmount\"]").attr("readOnly",false);
									} else{
										js.showErrorMessage("获取失败");
									}
								}
							});
						}
					}],
				dataInit: function(element){
					$(element).select2().on("change",function(){
						$(this).resetValid();
					});
				}
			}},
		{header:'${text("品牌")}', name:'brand', width:150, editable:true, edittype:'select', editoptions:{'maxlength':'10', 'class':'form-control','readonly':'true',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('car_pinpai')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue'
			}},
		{header:'${text("版型")}', name:'area', width:150, editable:true, edittype:'select', editoptions:{'maxlength':'64', 'class':'form-control','readonly':'true',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('car_quyu')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue'
			}},
		{header:'${text("车型")}', name:'model', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control','readonly':'true'}},
		{header:'${text("车辆")}', name:'carId', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'64', 'class':'form-control','readonly':'true'}},
		{header:'${text("车架号")}', name:'frameNumber', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control ','readonly':'true'}
		},
		{header:'${text("费用项目")}', name:'expenseItem', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control required',
				dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid();

					});
				}
			}
		},
		{header:'${text("应收金额")}', name:'apAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control required number',
		}},
		{header:'${text("实收金额")}', name:'actualApAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number','readonly':'true'}},
		{header:'${text("备注")}', name:'remarik', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'255', 'class':'form-control'}},
	],
	
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#tarDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {arDetailId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'tarDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,rowNumber,arDetailId,brand,area,model,carId,frameNumber,expenseItem,apAmount,actualApAmount,remarik,arId.arId,', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){

	}
});
</script>
<script>

	$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});
	function getByCuId() {
		if ($("#customerId").val() != "") {
			$.ajax({
				type: "POST",
				async : false,
				url: "${ctx}/ar/tar/getTarByCuId",
				data: {"customerId": $("#customerId").val()},
				dataType : "json",
				success: function (data) {
					$("#serviceOrderId").empty();
					if (data.length > 0) {
						for (var i = 0; i < data.length; i++) {
							var orderId = data[i].serviceOrderId;
							var orderCode = data[i].serviceOrderCode;
							var option = $("<option>").attr({
								"value" : orderId
							}).text(orderCode);
							$("#serviceOrderId").append(option);
						}
						$("#serviceOrderId option:first").prop("selected", 'selected');
						$("#serviceOrderId").change();
					} else{
						js.showErrorMessage("该客户没有订单信息！");
					}
				}
			});
		}
	}

	$(function () {


		if("${tar.isNewRecord}")//编辑
		{
			getByCuId();
		}else if("${!tar.isNewRecord}")//新增
		{
			$('#status1').val('1').trigger('change');
			$('#currency').val('1').trigger('change');
			$("#date").attr("value",js.formatDate(new Date()
					, 'yyyy-MM-dd'));
		}
          var str=":;";
		$.ajax({
			type: "POST",
			async : false,
			url: "${ctx}/ar/tar/getExpenseItem",
			// data: {"serviceOrderId": $("#serviceOrderId").val()},
			dataType : "json",
			success: function (data) {
				// $('#currency').val(data.currency).trigger('change');
					// $('#currency').val(data.currency);

					//根据订单编号赋值车架号
					if (data.length > 0) {
						for (var i = 0; i < data.length; i++) {
							var frameNumber1 =data[i].fixedExpenseName;

							var frameNumberId =data[i].fixedExpenseId;
							if(i!=data.length-1){
								str+=frameNumberId+":"+frameNumber1+";";
							}else {
								str+=frameNumberId+":"+frameNumber1;// 这里是option里面的 value:label
							}
						}

					} else{
						js.showErrorMessage("该订单没有车架号信息！");
					}
				$('#tarDetailDataGrid').setColProp('expenseItem', {editoptions: {value:str}});
				$('#tarDetailDataGrid').dataGrid('reloadGrid');

			}
		});

		$("table[id=\"tarDetailDataGrid\"]").on("blur","input[name='apAmount']",function () {
			var money=0;
			$("input[name=\"apAmount\"]").each(function () {
				if($(this).val()!=NaN&&$(this).val()!=null&&$(this).val()!=""){
				money=money+parseInt($(this).val());
			}

			})
			$("input[id=\"amount\"]").val(money);


		})
	})

	$(function () {
		if("!${tar.isNewRecord}"){
			$("#serviceOrderId").attr("readOnly",true)
			var list =$("#serviceOrderId").find("option")
			$(list[1]).prop("slected",true)
		}
	})
</script>